<template>
    <div v-if="user">
        <el-card class="box-card" style="width: 400px">
            <div slot="header" class="clearfix">
                <span>{{user.realName}}</span>
            </div>
            <div>
                <div style="display: flex;justify-content: center;margin-bottom: 8px;">
                    <img title="点击修改用户图像" :src="userFace" @click="upload"
                         style="width: 100px;height: 100px;border-radius: 50px;cursor: pointer;"
                         alt="">
                </div>
                <div>电话号码：
                    <el-tag style="margin-bottom: 8px">{{user.phone}}</el-tag>
                </div>
                <div>入院时间：
                    <el-tag style="margin-bottom: 8px">{{user.admissionTime | dateFormat('YYYY-MM-DD HH:mm:ss')}}</el-tag>
                </div>
                <div>所属机构：
                    <el-tag style="margin-bottom: 8px">{{user.institutionsName}}</el-tag>
                </div>
                <div>政治面貌：
                    <el-tag style="margin-bottom: 8px">{{user.landscapeName}}</el-tag>
                </div>
                <div>居住地址：
                    <el-tag style="margin-bottom: 8px">{{user.address}}</el-tag>
                </div>
                <div>用户标签：
                    <el-tag type="success" style="margin-right: 5px" v-for="(r,index) in user.roleList" :key="index">
                        {{r.chineseName}}
                    </el-tag>
                </div>
                <div style="display: flex;justify-content: space-around;margin-top: 10px">
                    <el-button type="primary" @click="showUpdateuserInfoView">修改信息</el-button>
                    <el-button type="danger" @click="showUpdatePasswdView">修改密码</el-button>
                </div>
            </div>
        </el-card>
        <el-dialog
                title="修改用户信息"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <table>
                    <tr>
                        <td>
                            <el-tag>用户账号：</el-tag>
                        </td>
                        <td>
                            <el-input v-model="user2.userName"></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <el-tag>身份证号：</el-tag>
                        </td>
                        <td>
                            <el-input v-model="user2.identityCard"></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <el-tag>手机号码：</el-tag>
                        </td>
                        <td>
                            <el-input v-model="user2.phone"></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <el-tag>用户地址：</el-tag>
                        </td>
                        <td>
                            <el-input v-model="user2.address"></el-input>
                        </td>
                    </tr>
                </table>
            </div>
            <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="updateuserInfo">确 定</el-button>
  </span>
        </el-dialog>
        <el-dialog
                title="修改密码"
                :visible.sync="passwdDialogVisible"
                width="30%">
            <div>
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                         class="demo-ruleForm">
                    <el-form-item label="请输入旧密码" prop="oldPass">
                        <el-input type="password" v-model="ruleForm.oldPass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="请输入新密码" prop="pass">
                        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="新确认密码" prop="checkPass">
                        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-dialog>
    </div>
</template>

<script>
  import eventBus from '@/eventBus.js'
    export default {
        name: "PersonalCenter",
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    oldPass: '',
                    pass: '',
                    checkPass: ''
                },
                rules: {
                    oldPass: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    pass: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPass: [
                        {validator: validatePass2, trigger: 'blur'}
                    ]
                },
                user: null,
                user2: null,
                dialogVisible: false,
                passwdDialogVisible: false,
                user3:JSON.parse(window.sessionStorage.getItem('userInfo')),
                userFace:'/static/images/face_default.png'
            }
        },
        mounted() {
            this.inituser();
            this.initInstanceAttachmentBatch();
            // 文件选择后的回调
            eventBus.$on('fileAdded', () => {
            });
            // 文件上传成功的回调
            eventBus.$on('fileSuccess', () => {
              this.initInstanceAttachmentBatch();
              // todo 刷新导航栏头像

            });
        },
        methods: {
            updateuserInfo() {
                this.putRequest("/account/BtAccountUser/update/BtAccountPersonUserInfo", this.user2).then(resp => {
                    if (resp) {
                        this.dialogVisible = false;
                        window.sessionStorage.removeItem('userInfo')
                        this.$router.replace('/')
                        this.$message.success('修改成功')
                        this.$store.dispatch('SETNAV',[]);
                    }
                })
            },
            showUpdateuserInfoView() {
                this.dialogVisible = true;
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.ruleForm.userId = this.user3.id;
                        this.putRequest("/account/BtAccountUser/update/password", this.ruleForm).then(resp => {
                            if (resp) {
                                window.sessionStorage.removeItem("userInfo")
                                this.$store.dispatch('SETNAV',[]);
                                this.$router.replace("/");
                                this.$message.success('修改成功')
                            }
                        })
                    } else {
                        return false;
                    }
                });
            },
            upload(){
                // 打开文件选择框
                eventBus.$emit('openUploader', {
                  instanceId: this.user3.id,
                  modelChunk: '用户头像'
                })
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            showUpdatePasswdView() {
                this.passwdDialogVisible = true;
            },
            inituser() {
                this.getRequest("/account/BtAccountUser/query/BtAccountUserInfoByUserId?userId="+this.user3.id).then(resp => {
                    if (resp) {
                        this.user = resp.data;
                        // 对象复制
                        this.user2 = Object.assign({}, this.user);
                    }
                })
            },
            initInstanceAttachmentBatch(){
              this.getRequest("/resources/InstanceAttachment/query/InstanceAttachmentBatch/"+this.user3.id).then(resp => {
                    if(resp){
                      this.userFace = resp.data.basePath+resp.data.btHiAttachments[0].attachmentPath;
                    }
              })
            }

        }
            }
</script>

<style scoped>
    img:before {
        content: ' ';
        display: block;
        position: relative;
        height: 32px;
        width: 32px;
        background-image: url(https://raw.githubusercontent.com/cheng-qiang/rfid/master/rfid-vue/rfid/src/images/loding.gif);}
    /* 修改信息输入框样式 */
    .el-input--small .el-input__inner {
        width: 300px;
        height: 32px;
        line-height: 32px;
    }
</style>
